﻿var page1 = new Sail.page({
    
	name     : "pageDetail",                      //定义页面对象的唯一标识
    haveNavi : false,                           //未包含导航条

    frames   : {
	
        name     : "editFrame",                     //该frame对象的唯一标识
        title  : "空调入库单数据维护",
        getUrlParameter : ["id"],
        getURL : "../../purchase.do?method=edit",
        postURL : "../../purchase.do?method=save",
        bindDataFirst : false,
        
      //-------------------------------------------------------------------------------
        //以JSON形式描述 frame 的构成(包含哪些 item)
        //-------------------------------------------------------------------------------
        items  : [
            {
                type  : "form", 
                name  : "formDetail",
                title : "空调入库单据",
                
                buttons : ["保存","关闭"],
            
                items : [
                    {
				        type:"input",
				        title:"单据编号",
				        allowBlank : false,
				        readOnly:true,
				        dataIndex:"dataset.billVo.billCode"
				    },{
				        type:"calendar",
				        title:"单据日期",
				        allowBlank : false,   
				        dataIndex:"dataset.billVo.billDate"
				    },{
                type:"combox", 
                title:"供应商",
                dataIndex : "dataset.billVo.otherDic2Id",
                nameIndex : "dataset.billVo.otherDic2Name",    
                allowBlank:true,
                root:"dataset.item",
                fieldText:"name",
                fieldValue:"id",
                url:"../../../DataDict?type=repairsSupplier"
            },{
      					type:"combox", title:"资金形式",
      					dataIndex : "dataset.billVo.otherDic1Id",
      					nameIndex : "dataset.billVo.otherDic1Name",    
      					allowBlank:false,
      					root:"dataset.dictionary",
      					fieldText:"name",
      					fieldValue:"id",
      					url: "../../dictionary.do?method=findDictionaryByTypeId&typeId="+COMBOX_PRICE_TYPE
	          },{
				        type:"textArea",
				        title:"备注",
				        dataIndex:"dataset.billVo.remark",
				        height:50   
				    }
                ],
            
                layoutConfig : {
                    columns : 2
                },
                layout : [
	                    {
				            items:[
				                {'name':'单据编号'},
				                {'name':'单据日期'}
				            ]
				        },{
				            items:[
				                {'name':'供应商'},
				                {'name':'资金形式'}
				            ]
				        },{
				            items:[
				                {name:'备注','colspan':2}
				            ]
				        }
                ]
            },
            {
                name      : "gridSub",
                type      : "editGrid",
                title     : "空调入库单明细",
              
                tbar      : ['新增','-','删除','-'],
                bbar      : false,
                allowBlank  : false,
                useDefautlFuncForadd : false,
                bindRelation   : {
                    root  :"dataset.billVo.billDetailVo",
                    id    :"id",
                    field : ["transactionDetailTypeId","materielId","unitId","sysFlag"],
                    col   :[
    						    {"header":"空调名称",   "width":200,   align:"left", "dataIndex":"materielName"},
    						    {"header":"供给形式",   "width":70, align:"left", "dataIndex":"otherDic1Id","nameIndex":"otherDic1Name"},
    						    {"header":"单位",   "width":60,   align:"left","dataIndex":"unitName"},
    						    {"header":"数量",   "width":80,   align:"left","dataIndex":"amount"},
    						    {"header":"金额",   "width":80,   align:"left","dataIndex":"money"},
    						    {"header":"单价",   "width":80,   align:"left","dataIndex":"price"}
                    ],
                    "editor":[
                        {},
                        {type:"comboBox",config:{url:"../../dictionary.do?method=findDictionaryByTypeId&typeId="+COMBOX_SUPPLY_TYPE,fieldText:"name",fieldValue:"id",root:"dataset.dictionary","allowBlank":false}},
                        {},
                        {type:"numberField", config:{allowBlank:false, maxLength:MAXLENGTH,decimalPrecision:DECIMALPRECISION}},
                        {type:"numberField", config:{allowBlank:false, maxLength:MAXLENGTH,decimalPrecision:DECIMALPRECISION}},
                        {}
                    ]
                }
            }
        ],
        
        //-------------------------------------------------------------------------------
        //【注】 添加 businessData 空结构
        //-------------------------------------------------------------------------------
        businessData : {
          "dataset":{
            "billVo":{
              "id":"",    
              "billDate":"",
              "operator":"",
              "transactionId":"",
              "transactionTypeId":TRANSACTION_TYPE_ADD,
              "billTypeId":BILL_TYPE_PURCHASE,
              "billCode":"",
              "fillTime":"",
              "billTypeName":"",
              "originEntityName":"",
              "targetEntityName":"",
              "originEntityId":"",
              "targetEntityId":"",        
              "otherDic1Id":"",
              "otherDic2Id":"",
              "otherDic3Id":"",
              "otherDic4Id":"",
              "otherDic5Id":"",
              "otherDic6Id":"",
              "otherDic7Id":"",
              "otherDic8Id":"",
              "otherDic9Id":"",
              "otherDic10Id":"",   
              "otherDic1Name":"",
              "otherDic2Name":"",
              "otherDic3Name":"",
              "otherDic4Name":"",
              "otherDic5Name":"",
              "otherDic6Name":"",
              "otherDic7Name":"",
              "otherDic8Name":"",
              "otherDic9Name":"",
              "otherDic10Name":"",
              "remark":"",
              "sysFlag":SYS_FLAG_USEING,
              "creator":"",
              "createTime":"",
              "lastModifier":"",
              "lastModifiedTime":"",
              "billDetailVo":
                {
                  "id":"",
                  "transactionDetailTypeId":TRANSACTION_DETAIL_TYPE_OUT,
                  "unitId":COMBOX_UNIT_VAULE,
                  "unitName":COMBOX_UNIT_NAME,
                  "materielId":"",
                  "materielName":"",
                  "price":"",
                  "money":"",
                  "amount":"",    
                  "stateId":"",
                  "stateName":"",
                  "weight":"",
                  "originEntityId":"",
                  "targetEntityId":"",
                  "originEntityName":"",
                  "targetEntityName":"",
                  "otherDic1Id":"",
                  "otherDic2Id":"",
                  "otherDic3Id":"",
                  "otherDic4Id":"",
                  "otherDic5Id":"",
                  "otherDic6Id":"",
                  "otherDic7Id":"",
                  "otherDic8Id":"",
                  "otherDic9Id":"",
                  "otherDic10Id":"",
                  "otherDic1Name":"",
                  "otherDic2Name":"",
                  "otherDic3Name":"",
                  "otherDic4Name":"",
                  "otherDic5Name":"",
                  "otherDic6Name":"",
                  "otherDic7Name":"",
                  "otherDic8Name":"",
                  "otherDic9Name":"",
                  "otherDic10Name":"",
                  "remark":"",
                  "sysFlag":SYS_FLAG_USEING,
                  "creator":"",
                  "createTime":"",
                  "lastModifier":"",
                  "lastModifiedTime":""
                }
              
            }
          }
        },

        //-------------------------------------------------------------------------------
        //【注】为当前 frame 的items 绑定事件
        //-------------------------------------------------------------------------------
        bindEvent2Items : function(){    
        	var obj_grid = $('gridSub'); 
            var obj_form  = $('formDetail');

            $('formDetail').getButton('保存').on('click',function(){
                this.saveBusiness("close");
            },this);
            
            
            $('formDetail').getButton('关闭').on('click',function(){
                this.close();
            },$('formDetail').ownerFrame);
            
            
            obj_grid.addListener2Editor("数量",'afteredit',function(column,editInfo){
            	computeRowPrice(editInfo);
            });
            
            obj_grid.addListener2Editor("金额",'afteredit',function(column,editInfo){
            	computeRowPrice(editInfo);
            });
            
            obj_grid.addButtonHandler('新增',function(){
                var items = Sail.open("materielSelector.html","list",{},600,520);
                if(!items || items.length===0) return;
                var obj_grid = this;
                var ids = [];
                for(var i=0;i<items.length;i++){
                	var record = obj_grid.createRecord.call(obj_grid);
		              record.editing = true;
                  record.set("transactionDetailTypeId",TRANSACTION_DETAIL_TYPE_OUT);
                  record.set("sysFlag",SYS_FLAG_USEING);
		              record.set('materielName', items[i].data.name || '');
		              record.set('materielId', items[i].data.id);
		              record.set('unitName', items[i].data.other2Name || '');
		              record.set('unitId', items[i].data.other2Id);
		              record.editing = false;
		              obj_grid.addRecord.call(obj_grid,record);
                }
            },obj_grid);
        },
        
        beforeBindData:function(){
        	if(this.getRequest("isCopyAdd") == 'true'){
        		var bill = this.businessData.dataset.billVo;
        		bill.id = "";
        		bill.transactionId = "";
        		bill.billDate = "";
        		bill.billCode = "";
        		bill.otherDic1Id = "";
        		var detailList = this.businessData.dataset.billVo.billDetailVo;
          	for (var i = 0; i < detailList.length; i++){
          		var detail = detailList[i];
          		detail.id = "";
          		detail.transactionId = "";
          	}
          }
        	return true;
        }
    },

	afterRun : function(){
		setInitValue();
	}
});

page1.run();

function computeRowPrice(gridRow){
	var amount = gridRow.record.get("amount");
  var money = gridRow.record.get("money");
  var price = 0;
  if (amount != 0){
    price = Math.round(100 * money / amount) / 100; //保留2位小数
  }
  $('gridSub').setFieldValueByDataIndex("price", gridRow.row, price);
}
/**
 * 设置单据编码
 * @return
 */
function setBillCode(){
	var url = "../../purchase.do?method=randomBillCode&billType="+BILL_TYPE_PURCHASE;
	Sail.postData(url,{},function(flag,getData){
		$('单据编号').setValue(getData.dataset.billCode);
	});
}

function setInitValue(){
	var beginDate = getMonthFirstDay();
	$('单据日期').setValue(beginDate);
	
	$('单据编号').disable();
	//$('录入人').disable();
	
	//增加时执行
	if(page1.isAdd() || page1.getRequest().data.isCopyAdd=='true') {
	  //setBillCode();
		$('单据编号').setValue("由系统自动生成");
		//$('录入人').setValue(this.getOpername());
  }
}